var login_user = null
var stuid = null
var mask = null

$(function() { // 初始化内容

	mui.plusReady();
	mui.init();
	mask = mui.createMask(); //遮罩层
	login_user = JSON.parse(localStorage.getItem('loginUser'));
	datetime_init();
	// tap_init();



	var star = window.sessionStorage.getItem('stu_id')

	stuid = JSON.parse(star)


	window.setTimeout(function() {
		initFollowUp()
	}, 500);
});




function saveFollowUp() {
	
	var redates = jQuery("#redates").val()
	if(redates == ""){
		var data = {}
		data['id'] = jQuery("#id").val();
		data['stuid'] = jQuery("#stuid").val();
		data['stuname'] = jQuery("#stuname").val();
		data['tel'] = jQuery("#tel").val();
		data['followstatus'] = jQuery("#followstatus").val();
		data['followinfo'] = jQuery("#followinfo").val();
		data['explain'] = jQuery("#explain").val();
		data['schoolid'] = login_user.schoolid;
		data['schoolname'] = login_user.schoolname;
		data['upschoolid'] = login_user.upschoolid;
		data['upschoolname'] = login_user.upschoolname;
		data['followid'] = login_user.id
		data['followname'] = login_user.name
		data['personalid'] = login_user.id
		data['personalname'] = login_user.name
	}else{
		var data = jQuery("#new_gj_form").serializeObject();;
		
		data['redate'] = jQuery("#dayredates").val();
		data['followid'] = login_user.id
		data['followname'] = login_user.name
		data['personalid'] = login_user.id
		data['personalname'] = login_user.name
	}


	
	console.log('表单上传信息=====' + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "fxgw/savegj",
		contentType: 'application/json',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	    		

		},
		success: function(result) {

			mask.close(); //显示遮罩层  	

			console.log("获取数据======" + JSON.stringify(result));
			mui.alert("保存成功")
			setTimeout(function() {

				mui.back()
			}, 1000)
		},

		error: function(e) {

			mask.close(); //显示遮罩层

			mui.alert(e)
			console.log(e);
		}
	});

}

//保存
function submitsave() {

	
	saveFollowUp();
	

}

function initFollowUp() {
	var data = {};
	data['id'] = stuid;
	console.log("初始化客户跟进信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "fxgw/loadgj",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {
			mask.close(); //显示遮罩层 	  
			console.log("跟进信息=====" + JSON.stringify(res));
			var stuMsg = res.stuList
			jQuery("#stuid").val(stuMsg.id)
			jQuery("#stuname").val(stuMsg.stuname)
			jQuery("#tel").val(stuMsg.stutel)
			jQuery("#schoolid").val(stuMsg.schoolid)
			jQuery("#schoolname").val(stuMsg.schoolname)
			jQuery("#upschoolid").val(stuMsg.upschoolid)
			jQuery("#upschoolname").val(stuMsg.upschoolname)
			var followList = res.followList
			var yyList = res.yyList
			jQuery("#id").val(res.follow_record.id)
			jQuery("#followstatus").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < followList.length; j++) {
				str += "<option value=\"" + followList[j].name + "\" >" + "LTC" + followList[j].name + "(" + followList[j].bak +
					")" + "</option>";
			}

			jQuery("#followstatus").append(str);



			jQuery("#followinfo").find("option").remove();

			var str1 = "<option value=\"\">请选择</option>";
			for (var j = 0; j < yyList.length; j++) {
				str1 += "<option value=\"" + yyList[j].name + "\" >" + yyList[j].name + "</option>";
			}

			jQuery("#followinfo").append(str1);

		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}


function loadDayredates(day) {
	var data = {};
	data['personalid'] = login_user.id;
	data['dayredate'] = day
	console.log("查询时间=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "newxlc_follow_record/gettranpersonnum",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {
			mask.close(); //显示遮罩层 	  
			console.log("时间信息=====" + JSON.stringify(res));
			let obj = res
			let msg = "";
			for (let i in obj) {
				if (obj[i] != "0" && obj[i] != 0) {
					msg += i + "有" + obj[i] + "人" + "\n"
				}
			}

			//if(){
			var btnArray = ['取消', '确认'];
			mui.confirm('是否提交跟进信息', msg, btnArray, function(e) {
				if (e.index == 1) {
					saveFollowUp()

				}
			})

		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}
